//对账回款统计
/**
 * 总视图
 */
interface IReceivableReconciliationViewModel extends IBaseViewModel<IReceivableReconciliationQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<receivableReconciliationListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 列表高度设置
     */
    Lheight:number; 
    
    //导出
    exportFunc:()=>void;
    //
    // selectYear:(years)=>void;

    yearList:any[];
    mounthList:any[];
    //查询的年
    year:number;
    //月
    mounth:number;
}

/**
 * 查询条件
 */
interface IReceivableReconciliationQueryTerms{
    /**
     * 客户单位
     */
    clientName:string;
    /**
     * 清算时间
     */
    time:string
}

class ReceivableReconciliationReturnController extends ListController<IReceivableReconciliationViewModel,IReceivableReconciliationQueryTerms>{
    gridApi:uiGrid.IGridApiOf<receivableReconciliationListItemResponse>;
    orderBy:string = "0";
    asc:boolean = true;
    skip:number=0;
    //年份下拉
    yearList:any[]=[];
    //月份下拉
    mounthList:any[]=[];

    constructor($scope:IReceivableReconciliationViewModel,private receivableService:IReceivableService,private $state:angular.ui.IStateService,
    private $location:ng.ILocationService,private $ngBootbox: BootboxService,private exportService:IexportService){
        super($scope);
        this.init();
    }

    init():void{
        super.init();

        // this.$scope.expectDis = false;
        // this.paginationOptions.pageSize = 50,
        this.$scope.queryTerms = {
            clientName:"",
            time:''
        }
        this.loadTimeDropDown();

        this.$scope.gridOptions = {
            data: [],
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            exporterMenuCsv: false,
            useExternalSorting: false,
            enableColumnResizing: true,
            exporterMenuPdf: false,
            enableGridMenu: false,
            columnDefs: [
                {displayName: "客户单位", field: 'clientName', enableColumnMenu: false, width: '12%', cellTooltip:true, headerTooltip: true},
                {displayName: "清算时间", field: 'liquidationTime', enableColumnMenu: false, width: '12%', cellTooltip:true, headerTooltip: true},
                {displayName: "上月业务发生额", field: 'lastMonBusinessOccurrence', enableColumnMenu: false, width: '12%', cellTooltip:true, headerTooltip: true},
                {displayName: "上月对账", field: 'lastMonAccount', enableColumnMenu: false, width: '12%', cellTooltip:true, headerTooltip: true},
                {displayName: '待对账', field: 'toBeAccount', enableColumnMenu: false, width: '10%', cellTooltip:true, headerTooltip: true},
                {displayName: '对账逾期', field: 'overdueAccount', enableColumnMenu: false, width: '10%', cellTooltip:true, headerTooltip: true},
                {displayName: '上月回款', field: 'lastMonBackMoney', enableColumnMenu: false, width: '12%', cellTooltip:true, headerTooltip: true,},
                {displayName: '待回款', field: 'toBeBackMoney', enableColumnMenu: false, width: '10%', enableSorting: false, cellTooltip:true, headerTooltip: true},
                {displayName: '回款逾期', field: 'overdueBackMoney', enableColumnMenu: false, width: '10%', cellTooltip:true, headerTooltip: true},
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
                // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                // gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        }
        
        this.$scope.exportFunc = this.exportFunc;
        // this.$scope.selectYear=this.selectYear;  
        this.query();
    }

    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    //时间选择change事件
    // selectYear(years) {
    //     var date = new Date;
    //     var yeare = date.getFullYear();
    //     var mounths = date.getMonth();
    //     // if()
    //     // alert('!!!')
    //     if (years == "0") {
    //         // this.mounthList = [{ value: 0, text: '8' }, { value: 1, text: '9' }, { value: 2, text: '10' }, { value: 3, text: '11' }, { value: 4, text: '12' }];
    //         // this.$scope.queryTerms.mounth = "0";
    //     } else if (years == (yeare - 2016)) {
    //         this.$scope.queryTerms.mounth = "0";
    //         this.$scope.mounthList = [];
    //         for (var startMounth =1; startMounth <= mounths+1; startMounth++) {
    //             var k = startMounth - 1;
    //             this.mounthList.push({ value: k, text: String(startMounth) });
    //             this.$scope.mounthList=this.mounthList
    //         }
    //     } else {
    //         this.$scope.queryTerms.mounth = "0";
    //         this.$scope.mounthList = [{ value: 0, text: '1' }, { value: 1, text: '2' }, { value: 2, text: '3' }, { value: 3, text: '4' }, { value: 4, text: '5' }, { value: 5, text: '6' }, { value: 6, text: '7' },
    //         { value: 7, text: '8' }, { value: 8, text: '9' }, { value: 9, text: '10' }, { value: 10, text: '11' }, { value: 11, text: '12' }];
    //     }
    // }
    
    /**
     * 导出报表
     */
    exportFunc=()=>{
        this.exportService.accountReturnExport(this.$scope.queryTerms.clientName,this.$scope.queryTerms.time);
    }

    loadData(queryTerms:IReceivableReconciliationQueryTerms,skip:number,count:number):void{
        if(this.$scope.year-1>=0){
            this.$scope.queryTerms.time=this.yearList[this.$scope.year-1].text  + '-' +this.$scope.mounth;
        }else{
            this.$scope.queryTerms.time =""
        }
        
        this.receivableService.getBigdataanalysisList(queryTerms.clientName,this.$scope.queryTerms.time,skip,count).then((result)=>{
            console.log('res',result)
            this.$scope.gridOptions.totalItems = result.data.total;
            this.$scope.gridOptions.data = result.data.data;
            this.$scope.Lheight = ((this.$scope.gridOptions.data.length * 39) + 80);
           
            if (skip == 0) {this.$scope.gridOptions.paginationCurrentPage = 1;}
            this.skip =skip;

            this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
        });
    }

    /**
     * 排序
     */
    // sortChange = (grid: uiGrid.IGridInstanceOf<FinanceReceivableListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<FinanceReceivableListItemResponse>>) => {
    //     if (sortColumns.length == 0) {
    //         this.reload();
    //     }else {
    //         let sortItem = sortColumns[0];
    //         switch (sortItem.name) {
    //             case "receivableSettleBillCode": {
    //                 this.orderBy = "1";
    //             } break;
    //             case "receivableCode": {
    //                 this.orderBy = "2";
    //             } break;
    //             case "feeType": {
    //                 this.orderBy = "3";
    //             } break;
    //             case "receivableSummary": {
    //                 this.orderBy = "4";
    //             } break;
    //             case "client": {
    //                 this.orderBy = "5";
    //             } break;
    //             case "settle": {
    //                 this.orderBy = "6";
    //             } break;
    //             case "originAddress": {
    //                 this.orderBy = "7";
    //             } break;
    //             case "realDeliveryTime": {
    //                 this.orderBy = "8";
    //             } break;
    //             case "goodsName":{
    //                 this.orderBy = "9";
    //             }break;
    //             case "receivablePrice": {
    //                 this.orderBy = "10";
    //             } break;
    //             case "contractNumber": {
    //                 this.orderBy = "11";
    //             } break;
    //             case "carCode":{
    //                 this.orderBy = "12";
    //             }break;
    //             case "settleStatus":{
    //                 this.orderBy = "13";
    //             }break;
    //             case "receiptStatus":{
    //                 this.orderBy = "14";
    //             }break;
    //         }
    //         this.asc = sortItem.sort.direction == "asc";
    //         this.reload();
    //     }
    // }

    /**
     * 状态下拉
     */
    loadTimeDropDown():void{
        var date = new Date;
        var year = date.getFullYear();
        var mounths = date.getMonth();

        for (var startYears = 2018; startYears <= year; startYears++) {
            var i = startYears - 2018;
            this.yearList.push({ value: i+1, text: String(startYears) });//
            this.$scope.yearList=this.yearList
            console.log('yearList',this.$scope.yearList)  
        }
        // for (var startMounth = 1; startMounth <= mounths+1; startMounth++) {
        //     var k = startMounth - 1;
        //     this.mounthList.push({ value: k, text: String(startMounth)+'月' });
        //     this.$scope.mounthList=this.mounthList
        // }
        this.$scope.mounthList = [{ value: 1, text: '1' }, { value: 2, text: '2' }, { value: 3, text: '3' }, { value: 4, text: '4' }, { value: 5, text: '5' }, { value: 6, text: '6' }, { value: 7, text: '7' },
            { value: 8, text: '8' }, { value: 9, text: '9' }, { value: 10, text: '10' }, { value: 11, text: '11' }, { value: 12, text: '12' }];
        
        this.$scope.mounth = mounths+1;
        // console.log('m',this.$scope.mounth)
        this.$scope.year = year - 2018 +1 ; 
        // console.log('y',this.$scope.year)
        // this.$scope.queryTerms.time=this.$scope.mounthList[this.$scope.mounth] + '-' + this.$scope.yearList[this.$scope.year]
    }

    
}

angular.module("tms").controller("receivableReconciliationReturnController", ["$scope","receivableService","$state","$location","$ngBootbox","exportService", ReceivableReconciliationReturnController]);